<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="js/template-web.js"></script>
    <script src="js/jquery.js"></script>
    <script>
        $(function () {
            $.ajax({//判断是否已登录
                url: "/user/isLogin",
                type:'post',
                dataType:'json',
                data: {
                    "phone": localStorage.getItem('localPhone'),
                },
                success:function (result) {
                    //查询是否登录后再查询所有商品数据
                    $.ajax({
                        url: "/product/findProducts",
                        type:'post',
                        dataType:'json',
                        success:function (data) {
                            //模板生成bookList
                            var html = template('productList',{"list": data});
                            //先进行清空
                            $("#products").empty();
                            $("#products").append(html);

                            if(result.username){
                                $('#login').html('欢迎您：'+result.username);
                            }else {
                                $('#login').html('你还没有登录');
                                //关闭生成订单和购物车添加功能
                                $('h1').attr("onclick","");
                                $('td').attr("onclick","");
                            }
                        }
                    });
                }
            });
        });

        function addCart(p) {
            $.ajax({
                url: 'product/addCart',
                type:'post',
                data:{
                    "phone": localStorage.getItem('localPhone'),
                    "productId":$(p).attr("productId")
                },
                dataType:'json',
                success:function (result) {
                    if(result.success){
                        alert("加入购物车成功！");
                    }else{
                        alert("加入购物车失败！");
                    }
                }
            });
        }

        function createOrder() {
            $.ajax({
                url: "/product/createOrder",
                type:'post',
                dataType:'json',
                data:{
                    "phone": localStorage.getItem('localPhone'),
                },
                success:function (rs) {
                    if(rs.success){
                        alert("订单生成成功:"+rs.storage)
                    }else {
                        alert("订单生成失败"+rs.storage)
                    }
                    location.href = '/product.html';
                }
            });
        }
    </script>
    <script  type="text/html" id="productList">
        <tr>
            <th>ID</th> <th>书名</th> <th>价格</th> <th>库存</th> <th>操作</th>
        </tr>
        {{each list product index}}
        <tr>
            <td>{{product.id}}</td> <td>{{product.name}}</td> <td>{{product.price}}</td> <td>{{product.num}}</td>
            <td onclick="addCart(this)" productId = "{{product.id}}">
                <a href="javascript:;">加入购物车</a>
            </td>
        </tr>
        {{/each}}
    </script>
</head>
<body>
    <div id="login"></div>
    <table border="1" id="products">
    </table><br>
    <a href="javascript:;"> <h1 onclick="createOrder()">生成订单</h1></a>
</body>
</html>